#include"utility.h"

int writeBit(FILE * fptr,unsigned char bit,unsigned char * whichbit,unsigned char * curbyte){

    if((*whichbit) == 0){

        *curbyte = 0;
    }
    // -左移（即将00000000当中的其中一位置为1）
    unsigned char temp = bit << (7 - (*whichbit));
    (*curbyte) |= temp;

    int value = 0;
    if((*whichbit) == 7){

        int ret = -1;
        ret = fwrite(fptr,curbyte,sizeof(unsigned char),1);
        if(ret == 1){
            // -写入到了文件
            value = 1;
        }else{
            // -尝试写入没有成功
            value = -1;
        }
    }
    // -没有写入到文件
    return value;
}

static void printByte(unsigned char onebyte){

    unsigned char mask = 0x80;  // -10000000
    while(mask > 0){

        printf("%d",(onebyte & mask) == mask);
        mask = mask >> 1;   // -右移 
    }
    printf("\n");
}
